import Ember from 'ember';
import InfiniteScroll from '../../infinite-scroll';

export default Ember.Controller.extend(InfiniteScroll,{
  dataLoader: Ember.inject.service("data-loader"),
  feedBus: Ember.inject.service("feed-bus"),
  infiniteContentPropertyName: "taskDetail",
  infiniteModelName: "task",
  infiniteContainerName:"taskDetailContainer",
  serviceItemChangeFlag:0,

  init:function(){
    Ember.run.schedule("afterRender",this,function() {
      $("a[name='processTab']").click(function(){
        console.log("tab click");
        $(this).toggleClass("hasSelect");
      });
    });
  },
  serviceItem:Ember.computed("taskId",function(){
    //从全局上下文服务里拿到外层传递的数据
    let serviceItem = this.get("feedBus").get("threadData");
    //与传参进行比较，一致才设置
    if(serviceItem.get("id")===this.get("taskId")){
      return serviceItem;
    }
  }),
  //选中的标签数据
  tagItemsObs: function(){
    //查找出执行标签的字典列表，然后比较
    let items = this.get("dataLoader").findDictList("exeStatus");
    let remark = this.get("serviceItem.remark");
    let remarkObj = null;
    if(remark&&remark.length>0){
        remarkObj = JSON.parse(remark);
    }
    if(remarkObj&&remarkObj.serviceTag){
      items.forEach(function(item){
        if(item.get("id")===remarkObj.serviceTag){
          //如果与字典数据匹配，则设置选中
          item.set("hasSelect",true);
        }else{
          item.set("hasSelect",false);
        }
      });
    }
    this.set("tagItems",items);
  }.observes("serviceItemChangeFlag"),

  actions:{
    tagChoice(tagId){
      //标签选中后与对象关联
      let remark = this.get("serviceItem.remark");
      let remarkObj = null;
      if(!remark){
        remarkObj = {};
      }else{
        remarkObj = JSON.parse(remark);
      }
      remarkObj.serviceTag = tagId;
      this.get("serviceItem").set("remark",JSON.stringify(remarkObj));
      this.incrementProperty("serviceItemChangeFlag");
    },
    backMain(){
      var mainpageController = App.lookup('controller:business.mainpage');
      mainpageController.switchMainPage('task-square');
    },
    saveDetail(){
      let _self = this;
      App.lookup("controller:business.mainpage.task-square").finishService(this.get("serviceItem"),function(){
        App.lookup("controller:business").popTorMsg("护理任务--" + _self.get("serviceItem").get("title") + ",已完成");
        App.lookup("controller:business.mainpage").switchMainPage("task-square");
      });
    }
  }
});
